package com.woke.adapter;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.bean.VipLevel;
import com.jakewharton.rxbinding2.view.RxView;
import com.zhongcai.online.BR;
import com.zhongcai.online.R;

import java.util.concurrent.TimeUnit;

import io.reactivex.functions.Consumer;
import me.drakeet.multitype.ItemViewProvider;

/**
 * 作者 ： 暗网
 * 创建时间：2017/2/17
 * 备注： TabHomeFragment 栏目
 */

public class UpgradeMemberShipViewProvider extends ItemViewProvider<VipLevel, UpgradeMemberShipViewProvider.ViewHolder> {

    private OnItemClickListener mItemClickListener;

    public UpgradeMemberShipViewProvider(OnItemClickListener listener) {
        mItemClickListener = listener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        ViewDataBinding binding = DataBindingUtil.inflate(inflater, R.layout.adapter_upgrade_member_ship_tiem, parent, false);

        ViewHolder holder = new ViewHolder(binding.getRoot());
        holder.setBinding(binding);
        return holder;
    }

    @Override
    protected void onBindViewHolder(@NonNull final ViewHolder holder, @NonNull final VipLevel vipLevel) {
        holder.getBinding().setVariable(BR.vipLevel, vipLevel);
        holder.getBinding().executePendingBindings();

        RxView.clicks(holder.upgradeBtn).throttleFirst(1, TimeUnit.SECONDS).subscribe(new Consumer<Object>() {
            @Override
            public void accept(Object o) throws Exception {
                if (mItemClickListener != null) {
                    mItemClickListener.OnItemClick(holder.itemView, vipLevel, getPosition(holder));
                }
            }
        });
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private ViewDataBinding binding;
        private Button upgradeBtn;

        public ViewHolder(View itemView) {
            super(itemView);
            upgradeBtn = (Button) itemView.findViewById(R.id.upgrade_btn);
        }

        public ViewDataBinding getBinding() {
            return binding;
        }

        public void setBinding(ViewDataBinding binding) {
            this.binding = binding;
        }


    }
}
